package com.example.speeddating.controller;

import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.example.speeddating.annotaion.LoginUser;
import com.example.speeddating.annotaion.TokenCheck;
import com.example.speeddating.common.BaseResponse;
import com.example.speeddating.common.RespGenerator;
import com.example.speeddating.entity.Comment;
import com.example.speeddating.entity.TokenEntity;
import com.example.speeddating.exception.CommonException;
import com.example.speeddating.service.CommentService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;
import org.springframework.stereotype.Controller;

import java.util.HashMap;

/**
 *
 * @author tina
 * @since 2023-06-28 11:14:33
 */
@RestController
@RequestMapping("/comment")
public class CommentController {

    @Autowired
    private CommentService commentService;

    /**
     * 获取评论面板
     * @param articleId
     * @param pageSize
     * @param pageNum
     * @return
     */
    @GetMapping("/detail")
    @TokenCheck(roles = "user")
    public BaseResponse selectCommentByArticleId(@LoginUser TokenEntity tokenEntity,@RequestParam Long articleId,@RequestParam(value = "pageSize", defaultValue = "3") int pageSize, @RequestParam(value = "pageNum", defaultValue = "1") int pageNum) {
        if (articleId == null || articleId <= 0){
           return RespGenerator.returnCommonError(new CommonException("请求参数错误"),null);
        }
        Page<Comment> page = new Page<>(pageNum, pageSize);
        return RespGenerator.returnOK(commentService.getTopCommentsByArticleId(page,articleId,tokenEntity.getId()));
    }


    /**
     * 点赞
     * @param id
     * @return
     */
    @GetMapping("/thump_num/{id}")
    public BaseResponse selectThumpNumById(@PathVariable("id") Long id){
        if (id == null || id <= 0){
            throw new CommonException("请求参数错误");
        }
        return RespGenerator.returnOK(commentService.selectArticleCommentNum(id));
    }

    /**
     * 添加评论
     * @param tokenEntity
     * @param jsonString
     * @return
     */
    @PostMapping("/add")
    @TokenCheck(roles = "user")
    public BaseResponse commentArticle(@LoginUser TokenEntity tokenEntity, @RequestBody HashMap<String,Object> jsonString){
        if (jsonString.get("parentCommentId")==null||jsonString.get("articleId")==null){
            throw new CommonException("请求参数错误");
        }
        Comment comment = new Comment();
        comment.setUserId(tokenEntity.getId());
        comment.setParentCommentId(Long.valueOf(jsonString.get("parentCommentId").toString()));
        comment.setArticleId(Long.valueOf(jsonString.get("articleId").toString()));
        comment.setContent(jsonString.get("content").toString());
        commentService.publishComment(comment);//插入 查询是对谁的评论返回给前端
        Comment res = commentService.selectComment(comment.getId());
        res.setArticleId(comment.getArticleId());
        return RespGenerator.returnOK(res);
    }

    /**
     * 删除评论
     * @param tokenEntity
     * @param id
     * @return
     */
    @DeleteMapping("/delete/{id}")
    @TokenCheck(roles = "user")
    public BaseResponse deleteCommentArticle(@LoginUser TokenEntity tokenEntity, @PathVariable Long id){
        if (id==null){
            throw new CommonException("请求参数错误");
        }
        commentService.deleteCommentById(id);
        return RespGenerator.returnOK("删除成功");
    }

}
